﻿using cludg.Domain.Content;
using FluentNHibernate.Mapping;
using cludg.Domain.Content.Stories;
using cludg.Domain.Users;
using cludg.Repository.NHibernate.Utils;

namespace cludg.Repository.NHibernate.Config.Content
{
	public class StoryMap : ClassMap<Story>
	{
		public StoryMap()
		{
			Table("Stories");
			Not.LazyLoad();
			Cache.ReadWrite();
			Id(x => x.Id).Length(50)
				.GeneratedBy.Custom<Utils.IdentifierGenerator>()
				.Default("");
			References<User>(x => x.Author)
				.Column("AuthorId")
				.Not.LazyLoad();
			References<User>(x => x.OfferAuthor)
				.Column("OfferAuthorId")
				.Not.LazyLoad();
			Map(x => x.Title).Length(200);
			Map(x => x.Body)
				.CustomSqlType("ntext");
			Map(x => x.CreationTime);
			Map(x => x.AnonymouseAuthorName).Length(50);
			References(x => x.Subject)
				.Column("SubjectId")
				.Cascade.SaveUpdate()
				.Not.LazyLoad();
			HasMany<Comment>(x => x.Comments)
				.KeyColumn("StoryId")
				.Access.ReadOnlyPropertyThroughLowerCaseField(Prefix.Underscore)
				.Cascade.All()
				.LazyLoad()
				.Inverse()
				.AsBag()
				.Cache.ReadWrite();
			HasMany<Tag>(x => x.Tags)
				.KeyColumn("ContentId")
				.Access.ReadOnlyPropertyThroughLowerCaseField(Prefix.Underscore)
				.Cascade.AllDeleteOrphan()
				.Not.LazyLoad()
				.AsBag()
				.Cache.ReadWrite();
			Map(x => x.UniqueName).Length(250);
			Map(x => x.Rate);
		}
	}
}
